/**
 * 
 */
package com.nari.baseinfo.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.nari.baseinfo.model.IHeatStation;
import com.nari.coreframe.dao.hibernate3.GenericDaoImpl;
import com.nari.platform.model.PageCond;
import com.nari.platform.model.ResultWithPageCond;

/**
 * 
 * @author Xiaopeng
 * @date 2013-8-1
 */
public class HeatStationDaoImpl extends GenericDaoImpl<IHeatStation, String> 
		implements IHeatStationDao {

	/**
	 * 根据所属城市ID查询换热站信息
	 * @param districtCityID 所属区级机构(可选)
	 * @param capitalCityID 所属市级机构(可选)
	 * @param provinceCityID 所属省级机构(可选)
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-23
	 */
	@SuppressWarnings("unchecked")
	public List<IHeatStation> queryStationByBelongCity(String districtCityID, 
			String capitalCityID, String provinceCityID) throws Exception {
		Criteria listCriteria = this.getCurrentSession().createCriteria(IHeatStation.class);
		//所属省级机构
		if(provinceCityID!=null && !"".equals(provinceCityID.trim())) {
			listCriteria.add(Restrictions.eq("provinceCityID", provinceCityID.trim()));
		}
		//所属市级机构
		if(capitalCityID!=null && !"".equals(capitalCityID.trim())) {
			listCriteria.add(Restrictions.eq("capitalCityID", capitalCityID.trim()));
		}
		//所属区级机构
		if(districtCityID!=null && !"".equals(districtCityID.trim())) {
			listCriteria.add(Restrictions.eq("districtCityID", districtCityID.trim()));
		}
		listCriteria.addOrder(Order.asc("stationName"));
		return listCriteria.list();
	}
	
	/**
	 * 分页查询换热站信息
	 * @param stationName 换热站名称(可选)
	 * @param districtCityID 所属区级机构(可选)
	 * @param capitalCityID 所属市级机构(可选)
	 * @param provinceCityID 所属省级机构(可选)
	 * @param pageCond
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-7
	 */
	@SuppressWarnings("unchecked")
	public ResultWithPageCond<IHeatStation> queryStationWithPageCond(String stationName, 
			String districtCityID, String capitalCityID, String provinceCityID, PageCond pageCond) throws Exception {
		Criteria listCriteria = this.getCurrentSession().createCriteria(IHeatStation.class);
		Criteria countCriteria = this.getCurrentSession().createCriteria(IHeatStation.class);
		//换热站名称
		if(stationName!=null && !"".equals(stationName.trim())) {
			listCriteria.add(Restrictions.ilike("stationName", stationName.trim().toLowerCase(), MatchMode.ANYWHERE));
			countCriteria.add(Restrictions.ilike("stationName", stationName.trim().toLowerCase(), MatchMode.ANYWHERE));
		}
		//所属省级机构
		if(provinceCityID!=null && !"".equals(provinceCityID.trim())) {
			listCriteria.add(Restrictions.eq("provinceCityID", provinceCityID.trim()));
		}
		//所属市级机构
		if(capitalCityID!=null && !"".equals(capitalCityID.trim())) {
			listCriteria.add(Restrictions.eq("capitalCityID", capitalCityID.trim()));
		}
		//所属区级机构
		if(districtCityID!=null && !"".equals(districtCityID.trim())) {
			listCriteria.add(Restrictions.eq("districtCityID", districtCityID.trim()));
		}
		//统计结果集个数
		int total = ((Long)countCriteria.setProjection(Projections.rowCount()).list().get(0)).intValue();
		//结果集排序
		listCriteria.addOrder(Order.asc("stationName"));
		//结果集分页
		listCriteria.setFirstResult(pageCond.getStart()).setMaxResults(pageCond.getLimit());
		//构造返回类型
		pageCond.setTotal(total);
		ResultWithPageCond<IHeatStation> stationPage = new ResultWithPageCond<IHeatStation>();
		stationPage.setList(listCriteria.list());
		stationPage.setPageCond(pageCond);
		return stationPage;
	}
}
